package com.ruoyi.chat.controller;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.jwyz.domain.Activity;
import com.ruoyi.jwyz.domain.Post;
import com.ruoyi.jwyz.domain.UserAttendActivity;
import com.ruoyi.jwyz.dto.TargetUserResult;
import com.ruoyi.wxlogin.domain.CUser;
import com.ruoyi.wxlogin.service.ICUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.chat.domain.Following;
import com.ruoyi.chat.service.IFollowingService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * 关注Controller
 *
 * @author lxp
 * @date 2023-03-31
 */
// Import necessary packages


@RestController
@RequestMapping("/chat/following")
@Api(tags = "关注管理")
public class FollowingController extends BaseController {

    @Autowired
    private IFollowingService followingService;

    @Autowired
    private ICUserService icUserService;

    /**
     * 查询关注列表
     */
    @ApiOperation("获取关注列表")
//    @PreAuthorize("@ss.hasPermi('chat:following:list')")
    @GetMapping("/list")
    public TableDataInfo list(Following following) {
        startPage();
        List<Following> list = followingService.selectFollowingList(following);
        return getDataTable(list);
    }

    /**
     * 导出关注列表
     */
    @ApiOperation("导出关注列表")
//    @PreAuthorize("@ss.hasPermi('chat:following:export')")
    @Log(title = "关注", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Following following) {
        List<Following> list = followingService.selectFollowingList(following);
        ExcelUtil<Following> util = new ExcelUtil<Following>(Following.class);
        util.exportExcel(response, list, "关注数据");
    }

    /**
     * 获取关注详细信息
     */
    @ApiOperation("获取关注详细信息")
//    @PreAuthorize("@ss.hasPermi('chat:following:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@ApiParam(value = "关注ID", required = true) @PathVariable("id") Long id) {
        return success(followingService.selectFollowingById(id));
    }

    /**
     * 新增关注
     */
    @ApiOperation("新增关注")
//    @PreAuthorize("@ss.hasPermi('chat:following:add')")
    @Log(title = "关注", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Following following) {
        return toAjax(followingService.insertFollowing(following));
    }

    /**
     * 修改关注
     */
    @ApiOperation("修改关注")
//    @PreAuthorize("@ss.hasPermi('chat:following:edit')")
    @Log(title = "关注", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Following following) {
        return toAjax(followingService.updateFollowing(following));
    }

    /**
     * 删除关注
     */
    @ApiOperation("删除关注")
//    @PreAuthorize("@ss.hasPermi('chat:following:remove')")
    @Log(title = "关注", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam(value = "关注IDs", required = true) @PathVariable Long[] ids) {
        return toAjax(followingService.deleteFollowingByIds(ids));
    }

    /**
     * 查询我的关注列表
     */
    @ApiOperation("查询我的关注列表-回显了关注的人信息")
    @GetMapping("/getFollowing/{uid}")
    public AjaxResult myFollowingList(@PathVariable("uid") String uId) {
        Long userId = Long.parseLong(uId);
        startPage();
        QueryWrapper<Following> queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        List<Following> postList = followingService.list(queryWrapper);
        List<CUser> cUserList = new ArrayList<>();
        for (Following following : postList) {
            QueryWrapper<CUser> activityQueryWrapper = new QueryWrapper<>();
            activityQueryWrapper.eq("id", following.getFollowingUserId());
            cUserList.addAll(icUserService.list(activityQueryWrapper));
        }
        if (cUserList.size() != 0) {
            return success(cUserList);
        }
        return error("没有查到您报名的相关活动信息");
    }

    /**
     * 查询我的关注列表-再进行取关
     */
    @ApiOperation("查询我的关注列表-再进行取关")
    @GetMapping("/offCustoms/{uid}/{fuid}")
    public AjaxResult offCustoms(@ApiParam(value = "用户ID", required = true) @PathVariable("uid") String uId ,
                                 @ApiParam(value = "被关注用户ID", required = true) @PathVariable("fuid")String fuId) {
        Long id = Long.parseLong(uId);
        Long fUid = Long.parseLong(fuId);
        QueryWrapper<Following> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("user_id", id)
                .and(wrapper -> wrapper.eq("following_user_id", fUid));
        Following following = followingService.getOne(queryWrapper);
        if (following == null) {
            return error("您未关注该用户");
        }
        boolean success = followingService.removeById(following.getId());
        if (success) {
            return success("执行成功");
        }
        return error("取关失败");
    }





}
